﻿using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
using Smartkernel.Net;

namespace Smartkernel.NUnitTest.Net
{
    [Category("Net")]
    [TestFixture]
    public class SmartFtpClientTest
    {
        [Test]
        public void Test()
        {
            //【01】上传文件（涉及方法：Upload）
            string ftpServer = "192.168.1.205";
            string port = "21";
            string userName = "admin";
            string password = "123";
            string targetFile = "FtpFile.txt";
            string localFilePath = @"C:\FtpFile.txt";
            using (StreamWriter streamWriter = File.CreateText(localFilePath))
            {
                for (int i = 0; i < 10000; i++)
                {
                    streamWriter.WriteLine("-----------------------------");
                }
            }
            SmartFtpClient.Upload(ftpServer, port, userName, password, null, localFilePath, targetFile);
            File.Delete(localFilePath);

            //【02】获得服务器文件列表（涉及方法：GetFileList）
            List<string> list = SmartFtpClient.GetFileList(ftpServer, port, userName, password, null);
            Assert.AreEqual(list.Count > 0, true);
            bool exists = false;
            list.ForEach(filePath => { if (filePath == targetFile) { exists = true; } });
            Assert.AreEqual(exists, true);

            //【03】判断是否存在指定的文件（涉及方法：Exists）
            Assert.AreEqual(SmartFtpClient.Exists(ftpServer, port, userName, password, null, targetFile), true);

            //【04】下载指定文件（涉及方法：）
            SmartFtpClient.Download(ftpServer, port, userName, password, null, localFilePath, targetFile);
            Assert.AreEqual(File.Exists(localFilePath), true);
            File.Delete(localFilePath);

            //【05】删除Ftp指定目录中的文件（涉及方法：Delete、Exists）
            List<string> deleteFile = new List<string>();
            deleteFile.Add(targetFile);
            SmartFtpClient.Delete(ftpServer, port, userName, password,null, deleteFile);
            Assert.AreEqual(SmartFtpClient.Exists(ftpServer, port, userName, password,  null, targetFile), false);

            //【06】上传文件（涉及方法：Upload）
            using (StreamWriter streamWriter = File.CreateText(localFilePath))
            {
                for (int i = 0; i < 10000; i++)
                {
                    streamWriter.WriteLine("-----------------------------");
                }
            }
            SmartFtpClient.Upload(null, localFilePath, targetFile);
            File.Delete(localFilePath);

            //【07】获得服务器文件列表（涉及方法：GetFileList）
            List<string> list2 = SmartFtpClient.GetFileList(null);
            Assert.AreEqual(list2.Count > 0, true);
            bool exists2 = false;
            list2.ForEach(filePath => { if (filePath == targetFile) { exists2 = true; } });
            Assert.AreEqual(exists2, true);

            //【08】判断是否存在指定的文件（涉及方法：Exists）
            Assert.AreEqual(SmartFtpClient.Exists(null, targetFile), true);

            //【09】下载指定文件（涉及方法：）
            SmartFtpClient.Download(null, localFilePath, targetFile);
            Assert.AreEqual(File.Exists(localFilePath), true);
            File.Delete(localFilePath);

            //【10】删除Ftp指定目录中的文件（涉及方法：Delete、Exists）
            List<string> deleteFile2 = new List<string>();
            deleteFile2.Add(targetFile);
            SmartFtpClient.Delete(null, deleteFile2);
            Assert.AreEqual(SmartFtpClient.Exists(null, targetFile), false);
        }
    }
}
